<!--
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-06-29 16:42:32
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-31 09:11:45
 * @FilePath: \cxl-h5\src\App.vue
-->
<template>
    <router-view v-slot="{ Component }">
        <transition name="routerFade" mode="out-in">
            <keep-alive include="register,detection,queryRecord,personalCenter,messageList">
                <component :is="Component" />
            </keep-alive>
        </transition>
    </router-view>
    <loginHintDialog />
</template>

<script setup lang="ts">
import { onMounted } from 'vue'
import { userStore } from '@/store/user.store'
import { getdWeixinSdkConfig } from './apis/wx'
import useWeixinSdk from './utils/WXSdk'
import { storage } from './store/sessionStorage'
import { KeepLoginCountDown } from './utils'
import { http } from './axios'
import { useRoute } from 'vue-router'
import loginHintDialog from './components/loginHintDialog.vue'

const store = userStore()
let token = storage.get('token')

const _useRoute = useRoute()

//获取微信sdk配置
const initWXSDKConfig = () => {
    //获取微信sdk配置
    getdWeixinSdkConfig().then((res: any) => {
        if (res.data.value === 200) {
            useWeixinSdk.init(
                {
                    timestamp: res.data.data.timestamp,
                    nonceStr: res.data.data.nonceStr,
                    signature: res.data.data.signature
                },
                () => {
                    // useWeixinSdk.disableShare()
                }
            )
        } else {
            const timer = setTimeout(() => {
                initWXSDKConfig()
                clearTimeout(timer)
            }, 500)
        }
    })
}

onMounted(() => {
    storage.set('loginStatus', 'false')
    store.setToken(token)

    initWXSDKConfig()

    const userInfo = storage.get('userInfo') ? JSON.parse(storage.get('userInfo')) : null
    if (userInfo) {
        store.isAdmin = !userInfo.isChild
    }

    console.log(
        'loginCountDownTimer=',
        storage.get('loginCountDownTimer'),
        ' keepLoginStatus=',
        storage.get('keepLoginStatus'),
        'loginStatus = ',
        storage.get('loginStatus')
    )
    //倒计时请求刷新token
    if (storage.get('loginCountDownTimer')) {
        if (JSON.parse(storage.get('keepLoginStatus'))) {
            new KeepLoginCountDown(Number(storage.get('expire')) / 1).start(() => {
                console.log('开始请求刷新token2')
                //router.replace('/login')
            })
        } else {
            //保持登录过期刷新或者重新进入跳转到登录页面
            console.log('relogin')
            http.token = ''
            storage.clear()
        }
    }
    if (storage.get('keepLoginStatus') !== 'true' && storage.get('loginStatus') === 'false') {
        console.log('update', _useRoute)

        // router.replace('/login')
        storage.clear()
    }
})
</script>

<style lang="scss"></style>
